package com.wangdao.mall.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 21:13 2020/4/29
 * @Modified By:
 */
public class HttpUtils {
    /**
     * 此方法用来把请求体中的参数（字符）数据转化为字符串
     * @param request
     * @return  请求体字符串
     * @throws IOException
     */
    public static String getRequestBody(HttpServletRequest request) throws IOException {
        //先将请求体写入到输入流
        ServletInputStream inputStream = request.getInputStream();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        //然后再写入到输出流
        while((len = inputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
        }
        return outputStream.toString("utf-8");
    }
}
